import tushare as ts
import pandas as pd
# 设置 token
token = 'c508869365fce05610e2cf264637f0473691e38fb80b3b21163bd150'
pro = ts.pro_api(token) # 初始化 pro 接口

# # 获取格力电器日线行情数据
# df_gldg = pro.daily(ts_code='000651.SZ', start_date='20090101',
#                                         end_date='20201021')

def pro_daily_stock(code_val='000651.SZ', start_val='20090101', end_val='20201028'):
    df_stock = pro.daily(ts_code=code_val, start_date=start_val, end_date=end_val)
    df_stock.trade_date = pd.DatetimeIndex(df_stock.trade_date) # 日期格式转换
    df_stock.set_index("trade_date", drop=True, inplace=True)
    df_stock.sort_index(inplace=True)
    df_stock.index = df_stock.index.set_names('Date')

    recon_data = {'Open': df_stock.open, 
            'Close':df_stock.close,
            'High':df_stock.high,
            'Low':df_stock.low,
            'Volume':df_stock.vol}
    df_recon = pd.DataFrame(recon_data)

    return df_recon

if __name__ == "__main__":
    df_stock = pro_daily_stock()
    print(df_stock.head())
    pass